//-----------------------------------------------------------------------
//  Form for displaying WebCam video
//
//  $File: WebCamForm.cs $ $Revision: 1 $
//
//-----------------------------------------------------------------------
using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Text;
using System.Windows.Forms;

using Microsoft.Ccr.Core;

namespace ProMRDS.Robotics.TeleOperation
{
    public partial class WebCamForm : Form
    {
        // This port is passed across to the constructor
        // It allows messages to be sent back to the main service
        WebCamFormEvents _eventsPort;
        // This is part of the main service State
        public GUIOptions options;

        #region Initialization and Shutdown

        public WebCamForm(WebCamFormEvents EventsPort, TeleOperationState state)
        {
            InitializeComponent();

            _eventsPort = EventsPort;

            // Take a copy of the options settings
            options = state.Options;

            // Position the window as requested
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(options.WebCamStartX, options.WebCamStartY);

        }

        private void WebCamForm_Load(object sender, EventArgs e)
        {
            _eventsPort.Post(new OnLoad(this));
        }

        private void WebCamForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _eventsPort.Post(new OnClosed(this));
        }

        #endregion

        private Bitmap _cameraImage;

        public Bitmap CameraImage
        {
            get { return _cameraImage; }
            set
            {
                _cameraImage = value;

                Image old = picCamera.Image;
                picCamera.Image = value;

                // Dispose of the old bitmap to save memory
                // (It will be garbage collected eventually, but this is faster)
                if (old != null)
                {
                    old.Dispose();
                }
            }
        }

        #region Keyboard Handlers

        private void WebCamForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch ((Keys)e.KeyValue)
            {
                case Keys.Up:
                    Forward();
                    e.Handled = true;
                    break;

                case Keys.Down:
                    Backward();
                    e.Handled = true;
                    break;

                case Keys.Left:
                    TurnLeft();
                    e.Handled = true;
                    break;

                case Keys.Right:
                    TurnRight();
                    e.Handled = true;
                    break;

                default:
                    break;
            }

        }

        private void WebCamForm_KeyUp(object sender, KeyEventArgs e)
        {
            Stop();
        }

        // Overriding the ProcessDialogKey handler allows us to trap the arrow keys.
        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                    Forward();
                    return true;

                case Keys.Down:
                    Backward();
                    return true;

                case Keys.Left:
                    TurnLeft();
                    return true;

                case Keys.Right:
                    TurnRight();
                    return true;

                default:
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }

        void Forward()
        {
            _eventsPort.Post(new OnMove(this, (int)options.MotionSpeed, (int)options.MotionSpeed));
        }

        void Backward()
        {
            _eventsPort.Post(new OnMove(this, (int)(-options.MotionSpeed), (int)(-options.MotionSpeed)));
        }

        void TurnLeft()
        {
            _eventsPort.Post(new OnMove(this, (int)(-options.MotionSpeed), (int)options.MotionSpeed));
        }

        void TurnRight()
        {
            _eventsPort.Post(new OnMove(this, (int)options.MotionSpeed, (int)(-options.MotionSpeed)));
        }

        void Stop()
        {
            _eventsPort.Post(new OnMove(this, 0, 0));
        }

        #endregion

    }

    #region Web Form Event PortSet and Messages

    // This is the PortSet for the different types of messages from the WinForm
    public class WebCamFormEvents
        : PortSet<
            OnLoad,         // Generic FormEvent
            OnClosed,       // Generic FormEvent
            OnMove          // Defined in DriveControl.cs
        >
    {
    }

    // This is the base class for all WebCam event messages
    // It is not really needed because there are no form-specific messages
    /*
    public class WebCamFormEvent : FormEvent
    {
        public WebCamFormEvent(WebCamForm form) :
            base((Form) form)
        {
        }
    }
     */

    #endregion

}